import { ApiProperty, Type } from '@midwayjs/swagger';
import { Rule, RuleType } from '@midwayjs/validate';
export enum Code {
  success = 0,
  fail = 1,
}
type Res<T> = {
  code: number;
  msg: string;
  data: T;
};
export function ApiRes<T,K = null>(
  ResourceCls: Type<T>
): Type<Res<T>> {
  class Response {
    @ApiProperty({ description: '响应码,0:成功,否则异常' })
    code: Code;

    @ApiProperty({ description: '响应信息' })
    msg: string;

    @ApiProperty({
      type: ResourceCls,
    })
    data: T;
  }

  return Response;
}

export class CommonRes {
  @ApiProperty({ description: '响应信息' })
  msg?: string;

  @ApiProperty()
  code?: Code;
}

export class PhoneDto {
  @ApiProperty({
    description: '手机号-11位',
    required: true,
  })
  @Rule(
    RuleType.string()
      .required()
      .pattern(/^1[3-9]\d{9}$/)
      .error(new Error('手机号码错误'))
  )
  phone: number;
}

export class CodeDto {
  @ApiProperty({ description: '验证码-6位', required: true })
  @Rule(RuleType.number().required().error(new Error('验证码不能为空')))
  code: number;
}

export class Flag {
  @ApiProperty({ description: '状态', example: true })
  flag: boolean;
}

export class FlagResDto extends ApiRes<Flag>(Flag) {}


export class Pagination {
  @ApiProperty({ description: '页码', required: true,example:1 })
  @Rule(RuleType.number().error(new Error('页码不能为空')))
  pageNo: number
  @ApiProperty({ description: '数量', required: true,example:10 })
  @Rule(RuleType.number().error(new Error('数量不能为空')))
  pageSize:number
}


export class PaginationDto extends Pagination {
  @ApiProperty({ description: '总数', required: true })
  total:number
}


export interface Domain{
  url:string
}


export interface Jwt{
  adminSecret:string
}
